<template>
<div class="login-views">
  <iForm class="login-views-form" ref="loginForm" :model="form" :rules="rules">
    <div class="login-views-title"><Icon type="log-in"></Icon>欢迎登录</div>

    <FormItem prop="account">
      <Input v-model="form.account" placeholder="请输入管理员账号">
        <span slot="prepend"><Icon :size="16" type="person"></Icon></span>
      </Input>
    </FormItem>

    <FormItem prop="password">
      <Input v-model="form.password" type="password" placeholder="请输入管理员密码">
        <span slot="prepend"><Icon :size="14" type="locked"></Icon></span>
      </Input>
    </FormItem>

    <FormItem><Button type="primary" long :loading="loading" @click="handleSubmit">登录</Button></FormItem>
  </iForm>
</div>
</template>

<script>
import { Form, Button, Icon, Input, Message } from 'iview';
import auth from '@/utils/auth';

export default {
  components: {
    [Form.name]: Form,
    [Form.Item.name]: Form.Item,
    [Input.name]: Input,
    [Button.name]: Button,
    [Icon.name]: Icon
  },
  data() {
    return {
      form: {
        account: '',
        password: ''
      },
      loading: false,
      rules: {
        account: [
          {required: true, message: '管理员账号不能为空!', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '管理员密码不能为空!', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    handleSubmit() {
      var _this = this;
      _this.$refs.loginForm.validate((valid) => {
        if(valid) {
          _this.loading = true;
          _this.$store.dispatch('loginByAccount', this.form).then(function(result) {
            if(result.status != 200) {
              Message.error('Status: ' + result.status + ', Message: ' + result.message);
            } else if(result.data.code != 0) {
              Message.warning('Message: ' + result.data.msg);
            } else {
              _this.$store.commit('SET_ACCOUNT', result.data.account.account);
              _this.$store.commit('SET_ACCOUNT_TOKEN', result.data.token);
              auth.setToken(result.data.token);

              Message.success({
                content: result.data.msg,
                onClose: function() {
                  _this.$router.push({ name: 'homeIndex' });
                }
              });
            }
          }).catch(function(result) {
            Message.error('Status: ' + result.status + ', Message: ' + result.message);
          });
        }

        _this.loading = false;
      });

      return false;
    }
  }
}
</script>

<style>
.login-views {
  position: fixed;
  height: 100%;
  width: 100%;
  background-color: #2d3a4b;
}
.login-views-form {
  position: absolute;
  left: 0;
  right: 0;
  width: 520px;
  padding: 35px 35px 15px 35px;
  margin: 120px auto;
}
.login-views-title {
  position: relative;
  font-size: 26px;
  font-weight: 400;
  color: #eee;
  margin: 0px auto 40px auto;
  text-align: center;
  font-weight: bold;
}
</style>